﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Collections.Generic;
using VedranKrizicPortfolio.Models;

namespace VedranKrizicPortfolio.Controllers
{
	public class HomeController : Controller
	{
		private static readonly vedranportfolioEntities _Vk = new vedranportfolioEntities();
		private static List<Article> _vkArticles = _Vk.Article.ToList();

		public ActionResult Index()
		{
			IEnumerable<Article> articles =
			_vkArticles.Where(a => a.PublishDate <= DateTime.Now && a.Type == (int)ArticleTypes.Portfolio && a.frontPage && !string.IsNullOrEmpty(a.CoverIMG)).OrderByDescending(a => a.PublishDate).Take(3);
			ViewBag.LatestNews = _vkArticles.Where(a => a.PublishDate <= DateTime.Now && a.Type == (int)ArticleTypes.News).OrderByDescending(a => a.PublishDate).First();
			if (articles.First().Text != null)
			{
				if (Request.IsAjaxRequest())
				{
					return PartialView("Index", articles);
				}
				else
				{
					return View(articles);
				}
			}
			return View("_Error");
		}

		public ActionResult About()
		{
            if (Request.IsAjaxRequest())
            {
                return PartialView(_Vk.Skills.OrderBy(s => s.Order).ToList());
            }
            else
            {
                return View(_Vk.Skills.OrderBy(s => s.Order).ToList());
            }
		}

		[HttpGet]
		public PartialViewResult MusicPlayer()
		{
			return PartialView("_MusicPlayer");
		}

		[HttpGet]
		public PartialViewResult Chat()
		{
			return PartialView("_Chat");
		}
	}
}